package org.dromara.system.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.BeanCopyUtils;
import org.dromara.system.mapper.SysRoleMenuMapper;
import org.dromara.system.pojo.bo.SysRoleMenuBO;
import org.dromara.system.pojo.entity.SysRoleMenuDO;
import org.dromara.system.pojo.qo.SysRoleMenuQO;
import org.dromara.system.pojo.vo.SysRoleMenuVO;
import org.dromara.system.service.SysRoleMenuService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色和菜单关联表 服务层实现。
 *
 * @author mkj
 * @since 2024-07-21
 */
@RequiredArgsConstructor
@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {

    private final SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public boolean add(SysRoleMenuBO sysRoleMenuBO) {
        SysRoleMenuDO sysRoleMenuDO = BeanCopyUtils.copy(sysRoleMenuBO, SysRoleMenuDO.class);
        return sysRoleMenuMapper.insert(sysRoleMenuDO) > 0;
    }

    @Override
    public boolean remove(Long id) {
        return sysRoleMenuMapper.deleteById(id) > 0;
    }

    @Override
    public boolean update(SysRoleMenuBO sysRoleMenuBO) {
        SysRoleMenuDO sysRoleMenuDO = BeanCopyUtils.copy(sysRoleMenuBO, SysRoleMenuDO.class);
        return sysRoleMenuMapper.update(sysRoleMenuDO) > 0;
    }

    @Override
    public List<SysRoleMenuVO> list(SysRoleMenuQO sysRoleMenuQO) {
        SysRoleMenuDO sysRoleMenuDO = BeanCopyUtils.copy(sysRoleMenuQO, SysRoleMenuDO.class);
        QueryWrapper queryWrapper = QueryWrapper.create(sysRoleMenuDO);
        return sysRoleMenuMapper.selectListByQueryAs(queryWrapper, SysRoleMenuVO.class);
    }

    @Override
    public SysRoleMenuVO getInfo(Long id) {
        SysRoleMenuDO sysRoleMenuDO = sysRoleMenuMapper.selectOneById(id);
        if (sysRoleMenuDO == null) {
            return null;
        }
        return BeanCopyUtils.copy(sysRoleMenuDO, SysRoleMenuVO.class);
    }

    @Override
    public Page<SysRoleMenuVO> page(SysRoleMenuQO page) {
        return sysRoleMenuMapper.paginateAs(page.getPageNumber(), page.getPageSize(), QueryWrapper.create(page), SysRoleMenuVO.class);
    }

}

